package com.pay.utils;

/**
 * Created by chenweiyi on 2018/1/30.
 */
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 日期工具类
 * @author chenweiyi
 *
 */
public final class DateUtil {

    /**
     * 	形如20180117131111000
     */
    public static final String YYYYMMDDHHmmssSSS = "yyyyMMddHHmmssSSS";

    /**
     * 形如2018-01-17 13:11:11.000
     */
    public static final String YYYY_MM_DD$HH_mm_ss$SSS="yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 形如2018-01-17 13:11:11
     */
    public static final String YYYY_MM_DD$HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 形如2018-01-17
     */
    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 形如2018-01
     */
    public static final String YYYY_MM = "yyyy-MM";

    /**
     * 形如2018
     */
    public static final String YYYY = "yyyy";



    /**
     * 获取周一00:00:00
     *
     * @param date
     * @return
     */
    public static Date getMonday(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dis = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            calendar.add(Calendar.DAY_OF_MONTH, (-7 - dis + 1) % 7);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar.getTime();
        }
        return null;
    }

    /**
     * 获取周日23:59:59
     * @param date
     * @return
     */
    public static Date getSunday(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dis = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            calendar.add(Calendar.DAY_OF_MONTH, (-dis + 7) % 7);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            return calendar.getTime();
        }
        return null;
    }

    /**
     * 日期格式转换
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date parse(String dateStr, String pattern) {
        if (dateStr != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                return sdf.parse(dateStr);
            } catch (Exception e) {
            }
        }
        return null;
    }


    /**
     * 调整日期
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date adjustDate(Date date, int amount) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, amount);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar.getTime();
        }
        return null;
    }


    /**
     * 调整时间
     * @param date
     * @param field
     * @param amount
     * @return
     */
    public static Date add(Date date,int field,int amount){
        if(date != null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(field, amount);
            return calendar.getTime();
        }
        return null;
    }

    /**
     * 调整时间
     * @param field
     * @param amount
     * @return
     */
    public static Date add(int field,int amount){
        return add(new Date(),field,amount);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                return sdf.format(date);
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return format(date, YYYY_MM_DD);
    }

    /**
     * 字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date parse(String dateStr) {
        return parse(dateStr, YYYY_MM_DD);
    }


}
